package com.comleader.operator.service;

import static com.comleader.operator.pojo.entity.table.InspectReportTableDef.INSPECT_REPORT;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.comleader.operator.mapper.InspectReportMapper;
import com.comleader.operator.pojo.entity.InspectRecordEntity;
import com.comleader.operator.pojo.entity.InspectReportEntity;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import cn.hutool.core.io.FileUtil;

/**
 * 检查报告表 服务层实现。
 *
 * @author 连新景
 * @since 1.0
 */
@Service
public class InspectReportService extends ServiceImpl<InspectReportMapper, InspectReportEntity> {

    @Resource
    private InspectRecordService inspectRecordService;

    public List<InspectReportEntity> listByRecordId(Integer recordId) {
        return list(QueryWrapper.create().where(INSPECT_REPORT.RECORD_ID.eq(recordId)).orderBy(INSPECT_REPORT.ID.asc()));
    }

    /**
     * @Description 删除记录和检查报告，以及生成的报告文件
     * @Auhtor 连新景
     * @Date 2024/07/25 15:20
     * @param recordId
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer recordId) {
        InspectRecordEntity inspectRecord = inspectRecordService.getById(recordId);
        inspectRecordService.removeById(recordId);
        remove(QueryWrapper.create().from(INSPECT_REPORT).where(INSPECT_REPORT.RECORD_ID.eq(recordId)));
        FileUtil.del(new File(inspectRecord.getReportFileDir()));
    }

}